﻿//********************************************************************************//
//创建日期：<创建日期，2017-7-25>
//创建作者：<郭登杰>
//功能说明：来函公共控件-月度调整申请信息
//********************************************************************************//

using System;
using System.Web.UI;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;
using Web.Class;

namespace Web.Controls.LAIHAN
{
    public partial class UCLHShenQingXX_YDTZ : UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        ///     来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        ///     来函流程
        /// </summary>
        public eLaiHanLCBH LaiHanLC { get; set; }

        /// <summary>
        ///     记录站点
        /// </summary>
        public string ZhanDianBH
        {
            get { return Convert.ToString(ViewState["ZhanDianBH"]); }
            set { ViewState["ZhanDianBH"] = value; }
        }

        /// <summary>
        ///     记录管理块
        /// </summary>
        public string GuanLiKBH
        {
            get { return Convert.ToString(ViewState["GuanLiKBH"]); }
            set { ViewState["GuanLiKBH"] = value; }
        }

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
                SetDisplay();
            }
        }

        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            //获取合同号

            //e.Result = e.Parameter + "|0|" + hetongh;
        }

        #endregion

        #region 方法

        /// <summary>
        ///     配置控件
        /// </summary>
        public void InitControl()
        {
            //this.txtJHL.Text = "0";
        }

        /// <summary>
        ///     设置控件启禁用
        /// </summary>
        /// <param name="enabled"></param>
        private void SetEnabled(bool enabled)
        {
            txtJHL.Enabled = enabled;
            chkBAI.Enabled = enabled;
        }

        /// <summary>
        ///     根据当前流程状态设置步骤显示
        /// </summary>
        private void SetDisplay()
        {
            //先隐藏所有
            txtJHL.Enabled = false;
            txtSYL.Enabled = false;
            txtYJH.Enabled = false;
            chkBAI.Enabled = false;
            //根据来函显示面板
            //根据来函类型及当前流程显示意意见量
            if (LaiHanLC < eLaiHanLCBH.登记)
                SetEnabled(true);
            else if (LaiHanLC >= eLaiHanLCBH.预登记) SetEnabled(false);
        }

        /// <summary>
        ///     提交验证
        /// </summary>
        ValidateRS IUCLHOperate.Validate()
        {
            var rs = new ValidateRS();
            if ((int) LaiHanLC < (int) eLaiHanLCBH.登记)
            {
                if (txtJHL.Text == "")
                {
                    rs.Msg = "请输入调整计划！";
                    return rs;
                }

                if (!PageTools.ValidateString(txtJHL.Text, PageTools.eValidateType.数字))
                {
                    rs.Msg = "请输入正确的计划量！";
                    return rs;
                }
            }

            rs.IsSuccess = true;
            return rs;
        }

        /// <summary>
        ///     清空数据
        /// </summary>
        public void ClearInfo()
        {
            txtJHL.Text = "";
            txtSYL.Text = "";
            txtYJH.Text = "";
        }
       
        /// <summary>
        ///     根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.S_YONGHUBH == null) return;
            if (laihanxx.ID == 0)
                LaiHanLC = eLaiHanLCBH.未登记;
            else
                LaiHanLC = (eLaiHanLCBH) Enum.Parse(typeof(eLaiHanLCBH), laihanxx.I_LIUCHENGBH.ToString());
            //根据当前来函流程显示控件
            SetDisplay();
            if (laihanxx.S_LAIHANBH == null)
            {
                int? syl, yjh;
                var ds = new SpHelper().P_YS_LH_YONGHUYSSJCX((int?) laihanxx.I_TZY, (int?) laihanxx.I_TZM,
                    laihanxx.S_YONGHUBH, out yjh, out syl);
                txtYJH.Text = yjh.ToString(); 
                txtSYL.Text = syl.ToString();
                txtJHL.Text = (Convert.ToDecimal(syl) - Convert.ToDecimal(yjh)).ToString();
                //if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                //{
                //    try
                //    {
                //        txtYJH.Text = Convert.ToString(ds.Tables[0].Rows[0]["I_YUANJIHUA"]);
                //        txtSYL.Text = Convert.ToString(ds.Tables[0].Rows[0]["I_SHIYONGSL"]);
                //        //chkBAI.Checked = true;
                //        this.txtJHL.Focus();
                //    }
                //    catch (Exception ex)
                //    {
                //        throw ex;
                //    }
                //}
            }
            else
            {
                //记录站点,管理块
                ZhanDianBH = laihanxx.S_ZHANDIANBH;
                GuanLiKBH = laihanxx.S_GUANLIKBH;
                var yuetz = new YS_LH_YUETIAOZProcess().SelectYS_LH_YUETIAOZsDynamic(
                    string.Format("S_LAIHANBH='{0}'", laihanxx.S_LAIHANBH), "  ID desc", null);
                if (yuetz != null && yuetz.Rows.Count > 0)
                {
                    txtJHL.Text = yuetz.Rows[0]["I_SHENQINGL"].ToString();
                    txtSYL.Text = yuetz.Rows[0]["I_SYL_SY"].ToString();
                    txtYJH.Text = yuetz.Rows[0]["I_JH_Y"].ToString();
                    //chkBAI.Checked = true;
                }
                int iSLYM = int.Parse(BLL.BusinessTool.YS_GETSETTINGSVALUE(eSETTINGS_KEYNAME.WATER_Y_M.ToString()));

                string month = laihanxx.I_TZM.ToString();
                if(month.Length<=1)
                {
                    month = "0" + month;
                }
                string ym = laihanxx.I_TZY.ToString()+month;

                if (iSLYM != int.Parse(ym))
                {
                    txtSYL.Text = "0";
                }
            }
        }
    
        /// <summary>
        ///     返回副表信息
        /// </summary>
        /// <returns></returns>
        public void GreateLHFB(YS_LH_LAIHANXX laihanxx)
        {
        }

        /// <summary>
        ///     生成退款信息
        /// </summary>
        /// <returns></returns>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            var yuetz = new YS_LH_YUETIAOZ();
            yuetz.ID = 0;
            var bllyue = new YS_LH_YUETIAOZProcess();
            var yuetiaoz = bllyue.SelectYS_LH_YUETIAOZsDynamic(
                string.Format(" S_LAIHANBH='{0}' AND I_JLZT=0", laihanxx.S_LAIHANBH, laihanxx.S_YONGHUBH), "", null);
            if (yuetiaoz != null && yuetiaoz.Rows.Count > 0) yuetz.ID = Convert.ToInt32(yuetiaoz.Rows[0]["ID"]);
            yuetz.S_CAOZUORQM = LoginInfo.CurrentUser.ElectronSign;
            yuetz.D_SHURURQ = BusinessTool.GetDataBaseDateTime();
            yuetz.I_JLZT = 0;
            yuetz.I_Y = laihanxx.I_TZY;
            yuetz.I_M = laihanxx.I_TZM;
            yuetz.S_LAIHANBH = laihanxx.S_LAIHANBH;
            yuetz.S_YONGHUBH = laihanxx.S_YONGHUBH;
            yuetz.S_ZHANDIANBH = laihanxx.S_ZHANDIANBH;
            yuetz.I_SHENQINGL = Convert.ToInt32(txtJHL.Text);
            yuetz.I_SHURUR = LoginInfo.CurrentUser.UserID;
            yuetz.I_SYL_SY = int.Parse(txtSYL.Text.Trim() == "" ? "0" : txtSYL.Text.Trim()); ;// int.Parse(txtSYL.Text);
            yuetz.I_JH_Y = int.Parse(txtYJH.Text.Trim() == "" ? "0" : txtYJH.Text.Trim());
            bllyue.InsertOrUpdate(yuetz);
        }

        #endregion
    }
}